package com.gegao.somethinginteresting.utils.fresco.builder;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.text.TextUtils;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.DraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.gegao.somethinginteresting.utils.fresco.DrawableUtils;
import com.gegao.somethinginteresting.utils.fresco.FRESCOURITYPE;
import com.gegao.somethinginteresting.utils.fresco.FrescoCacheUtils;
import com.gegao.somethinginteresting.utils.fresco.UriUtils;
import com.gegao.somethinginteresting.utils.fresco.callback.FrescoClearCompleteCallback;


/**
 * Fresco :gif加载
 * Created by liuchenghui on 2016/01/06.
 */
public class GifFrescoRequestBuilder extends FrescoRequestBuilder {

    protected DraweeView mDraweeView;
    private Context mContext;

    public GifFrescoRequestBuilder(DraweeView draweeView) {
        this.mDraweeView = draweeView;
    }

    public GifFrescoRequestBuilder clearPressDrawable(Context context) {
        this.mContext = context;
        return this;
    }


    @Override
    public void build() {
        if (mDraweeView == null || TextUtils.isEmpty(mUrl))
            return;
        //生成Uri
        Uri uri;
        if (mUrlType == null) {
            mUrlType = FRESCOURITYPE.HTTPURI;
        }
        uri = UriUtils.getUri(mUrl, mUrlType, mPackageName);
        //清除缓存
        if (isClearCache) {
            final Uri finalUri = uri;
            FrescoCacheUtils.clearUriCache(mUrlType, mUrl, mPackageName, new FrescoClearCompleteCallback() {
                @Override
                public void doSomething() {
                    request(finalUri);
                }
            });
        } else {
            request(uri);
        }
    }

    /**
     * 发起加载请求
     */
    private void request(Uri uri) {
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .build();
        //默认设置
        GenericDraweeHierarchy hierarchy;
        //是否设置占位图
        if (mHolder != null) {
            hierarchy = (GenericDraweeHierarchy) mDraweeView.getHierarchy();
            hierarchy.setPlaceholderImage(mHolder);
        }
        //清除点击的图片
        if (mContext != null) {
            GenericDraweeHierarchyBuilder mHierarchyBuilder = new GenericDraweeHierarchyBuilder(mContext.getResources());
            mHierarchyBuilder.setPressedStateOverlay(
                    DrawableUtils.maybeWrapWithScaleType(new BitmapDrawable()
                            , ScalingUtils.ScaleType.CENTER_INSIDE
                            , null));
            mHierarchyBuilder.setActualImageScaleType(ScalingUtils.ScaleType.FIT_CENTER);
            hierarchy = mHierarchyBuilder.build();
            mDraweeView.setHierarchy(hierarchy);
        }
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setAutoPlayAnimations(true)
                .build();
        //设置控制器
        mDraweeView.setController(controller);
    }

}

